// Itt Tallaght - HDIP- MiniProj - Lecturer Gary Clynch - Students Linda Kret x00115931 and Chris Pembroke x00115839
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using MiniProjChildrensBmiPercentiles.Models;

namespace MiniProjChildrensBmiPercentiles.Controllers
{
    public class AppBMIController : ApiController
    {
        private ChildrenDataEntities db = new ChildrenDataEntities();

        

        // GET: api/AppBMI/parent name
 
        [ResponseType(typeof(IEnumerable<Class5to7>))]
        public IHttpActionResult GetChildren(string parentName)  //get the children by their parent name
        {
            if ((parentName == null) || (parentName.Length > 50)) //if the string parent name sent is empty or longer then 50 
            {
                return BadRequest(); 
            }
            else if (parentName.ToLower()=="teacher") //if it's a teacher return all the children ordered by their names.
            {
                 IEnumerable<Class5to7> children =  db.Class5to7.ToList().OrderBy(kid=>kid.ChildName);
                return Ok(children);
            }
            else
            {
                //comparing the parent name sent and the DB name after removing spaces and make all lower case ,get the children according to the parent name ,in case match.
                IEnumerable<Class5to7> children =  db.Class5to7.ToList().Where(kid => kid.ParentName.ToLower().Replace(" ", "") == parentName.ToLower().Replace(" ", "")).OrderBy(kid=>kid.Age);
                //if no children found as no parent match return bad request.
                if (children == null)  
                {
                    
                    return BadRequest();
                 }

                //return Ok with the children matching the parent name
                return Ok(children);
            } 
          
        }
    } 
}
    